package com.redrose.demo.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity {
    protected T mBinding;
    protected final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater from = LayoutInflater.from(this);
        View view = from.inflate(getLayoutId(), null);
        setContentView(view);
        mBinding = DataBindingUtil.bind(view);
        initView();
        initData();
        initEvent();
        getLifecycle().addObserver(new MyObserver());
    }

    protected abstract void initEvent();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    protected <V extends ViewModel> V getActivityViewModel(Class<V> viewModel) {
        return new ViewModelProvider(this).get(viewModel);
    }

    protected <V extends AppCompatActivity> void openActivity(Class<V> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
}
